package com.msmall.community.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.community.ApplicationManager;
import com.msmall.community.team.model.TeamUser;
import com.msmall.community.team.service.TeamManager;

public class TeamTag extends TagSupport{
	
	private static final long serialVersionUID = 5519842521452264132L;

	private final Log log = LogFactory.getLog(TeamTag.class);
	private Long userid;
	
	private Long teamid;
	
	private String type;
	
	public int doStartTag() throws JspException {
		return (SKIP_BODY);
	}

	public int doEndTag() throws JspException {
		TeamManager manager = (TeamManager) ApplicationManager.appContext.getBean("teamManager");
		JspWriter writer = pageContext.getOut();
		HttpServletRequest req = (HttpServletRequest) super.pageContext.getRequest();
		HttpSession session = req.getSession();
		String info = "";
		try{
			//Long userid = (Long) session.getAttribute(Constants.USER_ID_KEY);
			if("status".equalsIgnoreCase(type)){
				TeamUser tu = manager.getTeamUser(userid, teamid);
				if(tu == null){
					info = "4";
				}else{
					info = tu.getStatus().toString();
				}
			}
		}catch(Exception e){
			log.error(e);
		}
		try {
			writer.print(info.toString());
		} catch (IOException e) {
			log.error(e);
		}
		return (SKIP_BODY);
	}

	public Long getUserid() {
		return userid;
	}

	public void setUserid(Long userid) {
		this.userid = userid;
	}

	public Long getTeamid() {
		return teamid;
	}

	public void setTeamid(Long teamid) {
		this.teamid = teamid;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}
